package com.dongdong.controller;

import com.alibaba.fastjson.JSON;
import com.dongdong.em.Result;
import com.dongdong.model.TbUser;
import com.dongdong.service.OrderService;
import com.dongdong.util.JWTUtil;
import com.dongdong.vo.AliPayVO;
import com.dongdong.vo.CartVO;
import com.dongdong.vo.OrderVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("order")
@RefreshScope    //热更新
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private JWTUtil jwtUtil;


    @GetMapping("submintOrder") //token   这个注解支支持英文
    public Result submitOrder(@RequestHeader String Autherizetion,@RequestParam Long addressId,@RequestParam String paymentType){//支付类型
        AliPayVO payVO = orderService.submitOrder(Autherizetion, addressId, paymentType);
        return Result.OK().setData(payVO);
    }

    //根据用户id  查询订单信息
    @GetMapping("getUserIdByOrderAndOrderItem")
    public Result getUserIdByOrderAndOrderItem(@RequestHeader String Autherizetion){
        //1.解析token获取用户id(仅获取id)
        Claims claims = jwtUtil.parseJwt(Autherizetion);
//        String userid = claims.getId();

        //(获取整个user对象)
        String subject = claims.getSubject();
        TbUser user = JSON.parseObject(subject, TbUser.class);

        //2.查询order表的userid   根据userid  获取到orderid
        List<OrderVO> orderVO = orderService.getOrderByUserId(user.getUsername());

        return Result.OK().setData(orderVO);
    }


    //付款成功  修改状态为 2 已付款
    @GetMapping("getOrderNoUpSatus")
    public int getOrderNoUpSatus(@RequestParam String trade_no){
        int i = orderService.getOrderNoUpSatus(trade_no);
        return i;
    }

    @GetMapping("getweather")
    public Result getweather(String city){
        return Result.OK();
    }

}
